package com.pks.datacenter.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.pks.datacenter.aspect.AspectLogTag;
import com.pks.datacenter.entity.api.ApiUser;
import com.pks.datacenter.entity.basic.DbInfo;
import com.pks.datacenter.entity.basic.Tag;
import com.pks.datacenter.service.api.ApiUserService;
import com.pks.datacenter.service.basic.TagService;
import com.pks.datacenter.utils.SUtilGeoPwd;
import com.pks.resultful.common.SRemoteResult;
import com.pks.util.UtilConvert;
import com.pks.util.UtilString;
import com.pks.web.springmvc.controller.BasicController;


@RestController
@RequestMapping(value = "/apiUser")
@Scope("prototype")
@Component("apiUserController")
@Api(tags = "接口账号")
public class ApiUserController extends BasicController {

	@Resource
	private ApiUserService apiUserService;
	@Resource
	private TagService tagService;

	@ApiOperation(value = "删除记录")
	@AspectLogTag(operType = "删除")
	@RequestMapping(value = "/delete" , method = RequestMethod.POST)
	public SRemoteResult<String> delete(HttpServletRequest request, String ids) {
		if (UtilString.isBlank(ids)) {
			return SRemoteResult.initErrorParamBlank(null);
		}
		if (StringUtils.isBlank(ids) || !ids.matches("^[0-9]+(,[0-9]+)*$")) {
			return SRemoteResult.initErrorParamInvalid(null);
		}
		List<Long> idList = UtilConvert.string2LongList(ids);
		apiUserService.deleteApiUsers(idList);
		return SRemoteResult.initSuccess(null);
	}
	
	@ApiOperation(value = "新增或修改")
	@AspectLogTag(operType = "修改")
	@RequestMapping(value = "/modify" , method = RequestMethod.POST)
	public SRemoteResult<String> modify(HttpServletRequest request, ApiUser obj) {
		if (obj == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		return apiUserService.insertOrModify(obj);
	}

	@ApiOperation(value = "获取详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "id", dataType = "long" , paramType = "query" )
	})
	@RequestMapping(value = "/get" , method = RequestMethod.GET)
	public SRemoteResult<ApiUser> findInfo(HttpServletRequest request, Long id) {
		if (id == null) {
			return SRemoteResult.initErrorParamMissing(null);
		}
		ApiUser obj = apiUserService.findById(id);
		if (obj == null) {
			return SRemoteResult.initErrorDataNone();
		}
		return SRemoteResult.initSuccess(obj);
	}

	@ApiOperation(value = "列表查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "account",value = "接口账号", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "password",value = "接口密码", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "state",value = "是否开启 1-是 0-否", dataType = "Integer" , paramType = "query" )
		,@ApiImplicitParam(name = "offset",value = "分页起始页（默认0）", dataType = "int" , paramType = "query" )
		,@ApiImplicitParam(name = "pagesize",value = "分页每页行数（默认10）", dataType = "int" , paramType = "query" )
	})
	@RequestMapping(value = "/list" , method = RequestMethod.POST)
	public SRemoteResult<List<ApiUser>> list(HttpServletRequest request,String account ,String accountLike ,String password,String name ,String nameLike ,Integer state) {
		Map<String, Object> qbuilder = new HashMap<String, Object>();
		if (UtilString.isNotBlank(account)) {
			qbuilder.put("account", account);
		}
		if (UtilString.isNotBlank(accountLike)) {
			qbuilder.put("accountLike", "%" + accountLike.trim() + "%");
		}
		if (UtilString.isNotBlank(password)) {
			qbuilder.put("password", password);
		}
		if (UtilString.isNotBlank(name)) {
			qbuilder.put("name", name);
		}
		if (UtilString.isNotBlank(nameLike)) {
			qbuilder.put("nameLike", "%" + nameLike.trim() + "%");
		}
		if (state != null) {
			qbuilder.put("state", state);
		}
		List<ApiUser> list = new ArrayList<ApiUser>();
		int total = 0;
		total = apiUserService.getTotalRows(qbuilder);
		if (total > 0) {
			list = apiUserService.findByPage(null, qbuilder);
			Map<Long, Tag> tagMap = tagService.findTagMap(Tag.TYPE_APIUSER);
			for (ApiUser obj : list) {
				if (obj.getTagId() != null) {
					Tag tag = tagMap.get(obj.getTagId());
					if (tag != null) {
						obj.setShow_tagName(tag.getName());
					}
				}
			}
		}
		return SRemoteResult.initPageList(list, total);
	}
	
	@AspectLogTag(operType = "修改密码")
	@RequestMapping(value = "/resetPwd" , method = RequestMethod.POST)
	public SRemoteResult<String> resetPwd(HttpServletRequest request,Long id) {
		String pwd = SUtilGeoPwd.geoApiUserPwd();
		if (id != null) { 
			return apiUserService.updateForResetPwd(id,pwd);
		}
		return SRemoteResult.initSuccess(pwd);
	}


//    @ApiOperation(value = "数据查询（不分页）")
//    @ApiImplicitParams({
//        @ApiImplicitParam(name = "account",value = "接口账号", dataType = "String" , paramType = "query" )
//        ,@ApiImplicitParam(name = "password",value = "接口密码", dataType = "String" , paramType = "query" )
//        ,@ApiImplicitParam(name = "state",value = "是否开启 1-是 0-否", dataType = "Integer" , paramType = "query" )
//    })
//    @RequestMapping(value = "/data" , method = RequestMethod.POST)
//    public SRemoteResult<List<ApiUser>> data(HttpServletRequest request,String account ,String password ,Integer state  ) {
//        Map<String, Object> qbuilder = new HashMap<String, Object>();
//        if (UtilString.isNotBlank(account)) {
//            qbuilder.put("account", account);
//        }
//        if (UtilString.isNotBlank(password)) {
//            qbuilder.put("password", password);
//        }
//        if (state != null) {
//            qbuilder.put("state", state);
//        }
//        List<ApiUser> list = new ArrayList<ApiUser>();
//        list = apiUserService.find(null, qbuilder, null);
//        return SRemoteResult.initPageList(list, null);
//    }


}
